import 'package:flutter/material.dart';

class CounterDemo extends StatefulWidget {
  @override
  _CounterDemoState createState() => _CounterDemoState();
}

class _CounterDemoState extends State<CounterDemo> {
  int _counter = 0;
  double _fontSize = 16.0;
  Color _textColor = Colors.black;
  bool _isFavorite = false;

  // 初始化状态
  @override
  void initState() {
    super.initState();
    print('初始化状态');
    // 可以在这里进行一些初始化操作
  }

  // 构建 widget
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('StatefulWidget 示例'),
        backgroundColor: _isFavorite ? Colors.pink : Colors.blue,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '你已经点击了按钮:',
              style: TextStyle(fontSize: 20),
            ),
            Text(
              '$_counter 次',
              style: TextStyle(
                fontSize: _fontSize,
                color: _textColor,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: 20),
            Text(
              '字体大小: ${_fontSize.toStringAsFixed(1)}',
              style: TextStyle(fontSize: 16),
            ),
            Text(
              '当前颜色: ${_getColorName(_textColor)}',
              style: TextStyle(fontSize: 16),
            ),
          ],
        ),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: '增加计数',
            child: Icon(Icons.add),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _decrementCounter,
            tooltip: '减少计数',
            child: Icon(Icons.remove),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _resetCounter,
            tooltip: '重置计数',
            child: Icon(Icons.refresh),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _changeFontSize,
            tooltip: '改变字体大小',
            child: Icon(Icons.text_fields),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _changeColor,
            tooltip: '改变文字颜色',
            child: Icon(Icons.color_lens),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _toggleFavorite,
            tooltip: '收藏',
            backgroundColor: _isFavorite ? Colors.pink : Colors.grey,
            child: Icon(_isFavorite ? Icons.favorite : Icons.favorite_border),
          ),
        ],
      ),
    );
  }

  // 增加计数器
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  // 减少计数器
  void _decrementCounter() {
    setState(() {
      if (_counter > 0) {
        _counter--;
      }
    });
  }

  // 重置计数器
  void _resetCounter() {
    setState(() {
      _counter = 0;
      _fontSize = 16.0;
      _textColor = Colors.black;
      _isFavorite = false;
    });
  }

  // 改变字体大小
  void _changeFontSize() {
    setState(() {
      _fontSize = 16.0 + (_counter % 10);
    });
  }

  // 改变文字颜色
  void _changeColor() {
    setState(() {
      List<Color> colors = [
        Colors.black,
        Colors.red,
        Colors.blue,
        Colors.green,
        Colors.purple,
        Colors.orange
      ];
      _textColor = colors[_counter % colors.length];
    });
  }

  // 切换收藏状态
  void _toggleFavorite() {
    setState(() {
      _isFavorite = !_isFavorite;
    });
  }

  // 获取颜色名称
  String _getColorName(Color color) {
    if (color == Colors.black) return '黑色';
    if (color == Colors.red) return '红色';
    if (color == Colors.blue) return '蓝色';
    if (color == Colors.green) return '绿色';
    if (color == Colors.purple) return '紫色';
    if (color == Colors.orange) return '橙色';
    return '其他颜色';
  }

  // 当 widget 参数发生变化时调用
  @override
  void didUpdateWidget(covariant CounterDemo oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('Widget 参数更新');
  }

  // 当对象从树中移除时调用（但在 dispose 之前）
  @override
  void deactivate() {
    super.deactivate();
    print('Widget 被停用');
  }

  // 清理资源
  @override
  void dispose() {
    print('清理资源');
    super.dispose();
  }
}

// 使用示例
void main() {
  runApp(MaterialApp(
    home: CounterDemo(),
  ));
}
